from .models import *
import random
import pytz
from MyBlog.settings import QUOTATIONS


def custom_proc(request):
    tags = Tag.objects.all()
    catagories = Catagory.objects.all()
    archives = Archive.objects.all()
    articles_rank = Article.objects.prefetch_related().order_by('-visit_count')[:10]
    article_sum = sum([catagory.article_set.count() for catagory in catagories])
    links = Link.objects.all()
    quotation = random.choice(QUOTATIONS)

    records = ReadRecord.objects.all().order_by('-date_time')[:5]
    comments = Comment.objects.all().order_by('-submit_time')
    latest_comments = comments[:5]

    return {'tags': tags, 'catagories': catagories, 'archives': archives, 'sum': article_sum, 'rank': articles_rank,
            'links': links, 'quotation': quotation, 'records': records, 'comments': latest_comments,
            'record_sum': len(comments),}


def admin_proc(request):
    latest_comments = Comment.objects.all().order_by('-submit_time')[:7]
    today_comments = [comment for comment in latest_comments if
                      comment.submit_time.astimezone(pytz.timezone('Asia/Shanghai')).date() == datetime.date.today()]
    admin = request.user
    head_list = ['baozou1.jpg', 'baozou2.jpg', 'baozou3.jpg', 'baozou4.jpg']
    return {'latest_comments': latest_comments, 'today_comments': today_comments, 'admin': admin,
            'head_list': head_list}
